package cn.springmvc.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.springmvc.model.Step1;
import cn.springmvc.model.Step2;
import cn.springmvc.model.Step3;
import cn.springmvc.model.Step4;
import cn.springmvc.model.Step5;
import cn.springmvc.model.Step6;
import cn.springmvc.model.Step7;
import cn.springmvc.model.Step8;
import cn.springmvc.service.ExcelServiceImpl;
import cn.springmvc.service.Step1Service;
import cn.springmvc.service.Step2Service;
import cn.springmvc.service.Step3Service;
import cn.springmvc.service.Step4Service;
import cn.springmvc.service.Step5Service;
import cn.springmvc.service.Step6Service;
import cn.springmvc.service.Step7Service;
import cn.springmvc.service.Step8Service;

@Controller
public class ExcelController {
	@Autowired
	private Step1Service step1Service;
	@Autowired
	private Step2Service step2Service;
	@Autowired
	private Step3Service step3Service;
	@Autowired
	private Step4Service step4Service;
	@Autowired
	private Step5Service step5Service;
	@Autowired
	private Step6Service step6Service;
	@Autowired
	private Step7Service step7Service;
	@Autowired
	private Step8Service step8Service;
@Autowired
private ExcelServiceImpl<Step1> ex1;
@Autowired
private ExcelServiceImpl<Step2> ex2;
@Autowired
private ExcelServiceImpl<Step3> ex3;
@Autowired
private ExcelServiceImpl<Step4> ex4;
@Autowired
private ExcelServiceImpl<Step5> ex5;
@Autowired
private ExcelServiceImpl<Step6> ex6;
@Autowired
private ExcelServiceImpl<Step7> ex7;
@Autowired
private ExcelServiceImpl<Step8> ex8;
@RequestMapping(value="/getExcel",method=RequestMethod.POST)
@ResponseBody
public void getExcel(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException{
	int year=Integer.parseInt(request.getParameter("year"));
	String docsPath = request.getSession().getServletContext().getRealPath("/");
	final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
	String[] headers1 = { "id" ,"名字","临时工", "技术工", "消耗", "年份","编号" };
	String[] headers2 = { "id", "名字","临时工", "技术工",  "8.8kw拖拉机","4.4-5.9kw拖拉机","2.2-3.7kw拖拉机","年份" ,"编号"};
	String[] headers3 = { "id", "临时工", "技术工", "地膜消耗", "年份","编号" };
	String[] headers4 = { "id", "名字","临时工", "技术工", "水量消耗", "年份","编号" };
	String[] headers5= { "id", "名字","临时工", "技术工", "有机无机复合肥", "俄罗斯高浓度肥","尿素","磷酸二铵","硫酸钾","磷肥","有机肥" ,"年份","编号"};
	String[] headers6 = { "id","名字", "临时工", "技术工", "除草剂","灭虫剂", "年份","编号" };
	String[] headers7 = { "id", "名字","临时工", "技术工", "竹竿15mm","竹竿30mm","麻绳", "年份","编号" };
	String[] headers8 = { "id", "名字","临时工", "技术工", "年份","编号" };
	List<Step1> stp1=step1Service.getStep1ByNianfen(year);
	List<Step2> stp2=step2Service.getStep2ByNianfen(year);
	List<Step3> stp3=new ArrayList<Step3>();
	stp3.add(step3Service.getStep3ByNianfen(year));
	List<Step4> stp4=step4Service.getStep4ByNianfen(year);
	List<Step5> stp5=step5Service.getStep5ByNianfen(year);
	List<Step6> stp6=step6Service.getStep6ByNianfen(year);
	List<Step7> stp7=step7Service.getStep7ByNianfen(year);
	List<Step8> stp8=step8Service.getStep8ByNianfen(year);
	OutputStream out = new FileOutputStream(docsPath + FILE_SEPARATOR+ "export2003_b.xls");
	
//	ex1.exportExcel("测试", headers, stp1, out, "yyyy-MM-dd");
	HSSFWorkbook workbook = new HSSFWorkbook();
	ex1.produceSheetData("幼苗种植", headers1, stp1, workbook, "yyyy-MM-dd");
	ex2.produceSheetData("犁地", headers2, stp2, workbook, "yyyy-MM-dd");
	ex3.produceSheetData("地膜", headers3, stp3, workbook, "yyyy-MM-dd");
	ex4.produceSheetData("灌溉", headers4, stp4, workbook, "yyyy-MM-dd");
	ex5.produceSheetData("施肥", headers5, stp5, workbook, "yyyy-MM-dd");
	ex6.produceSheetData("喷药", headers6, stp6, workbook, "yyyy-MM-dd");
    ex7.produceSheetData("搭架", headers7, stp7, workbook, "yyyy-MM-dd");
 	ex8.produceSheetData("采摘", headers8, stp8, workbook, "yyyy-MM-dd");
	try {
		workbook.write(out);
	} catch (IOException e) {
		e.printStackTrace();
	}
	download(docsPath + FILE_SEPARATOR+ "export2003_b.xls",response);
}

private void download(String path, HttpServletResponse response) {
	try {
		// path是指欲下载的文件的路径。
		File file = new File(path);
		// 取得文件名。
		String filename = file.getName();
		// 以流的形式下载文件。
		InputStream fis = new BufferedInputStream(new FileInputStream(path));
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		fis.close();
		// 清空response
		response.reset();
		// 设置response的Header
		response.addHeader("Content-Disposition", "attachment;filename="
				+ new String(filename.getBytes()));
		response.addHeader("Content-Length", "" + file.length());
		OutputStream toClient = new BufferedOutputStream(
				response.getOutputStream());
		response.setContentType("application/vnd.ms-excel;charset=gb2312");
		toClient.write(buffer);
		toClient.flush();
		toClient.close();
	} catch (IOException ex) {
		ex.printStackTrace();
	}
}

}
